﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Entity;
using BLL;

namespace Web.admin.InnerManage
{
    public partial class AddInnerPicture : System.Web.UI.Page
    {
        private PhotoService photoService = new PhotoService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string operation = Request["OPERATION"];
                if (operation == null)
                {
                    string procId = Request["ProcedureId"];
                    ProcedureId.Value = procId;
                    FileTR.Visible = true;
                    ReturnLink.NavigateUrl = "InnerPictures.aspx?id=" + procId;
                    return;
                }
                string pictureId = Request["id"];
                if (pictureId == null)
                    return;

                if (operation == "Modify")
                {
                    ModifyPicture(pictureId);
                }
                else if (operation == "Delete")
                {
                    DeletePicture(pictureId);
                }
            }
        }

        protected void ModifyPicture(string pictureId)
        {
            PictureId.Value = pictureId;

            BoPhotos photo = photoService.getPhoto(pictureId);

            ReturnLink.NavigateUrl = "InnerPictures.aspx?id=" + photo.PHO_PROCEDURES;

            txtTitle.Text = photo.PHO_TITLE;
            txtDate.Text = photo.PHO_DATE.ToString();
            Image1.ImageUrl = "/UploadImages/" + photo.PHO_URL;

            FileTR.Visible = false;
        }

        protected void DeletePicture(string pictureId)
        {
            BoPhotos photo = photoService.getPhoto(pictureId);
            DeleteImageFile(photo);
            photoService.DeletePhoto(photo);
            Response.Redirect("InnerPictures.aspx?id=" + photo.PHO_PROCEDURES);
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            BoPhotos photo = null;
            if (PictureId.Value == "")
            {
                photo = new BoPhotos();
                photo.PHO_PROCEDURES = ProcedureId.Value;
            }
            else
                photo = photoService.getPhoto(PictureId.Value);

            photo.PHO_TITLE = txtTitle.Text;
            photo.PHO_DATE = DateTime.Parse(txtDate.Text);

            if (PictureId.Value == "")
            {
                photo.PHO_URL = SaveImageFile();
                photoService.CreatPhoto(photo);
            }
            else
                photoService.ModifyPhoto(photo);

            Response.Redirect("InnerPictures.aspx?id=" + photo.PHO_PROCEDURES);
        }

        public String SaveImageFile()
        {
            Boolean fileOK = false;        
            String path = Server.MapPath("~/UploadImages/");        
            if (FileUpload1.HasFile)         
            {            
                String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();            
                String[] allowedExtensions = {".gif", ".png", ".jpeg", ".jpg"};          
                for (int i = 0; i < allowedExtensions.Length; i++)          
                {               
                    if (fileExtension == allowedExtensions[i])               
                    {                    
                        fileOK = true;               
                    }          
                }        
            }         
            if (fileOK)        
            {            
                try            
                {
                    String pre = Guid.NewGuid().ToString();
                    FileUpload1.PostedFile.SaveAs(path + pre + "_" + FileUpload1.FileName);
                    return pre + "_" + FileUpload1.FileName;
                }            
                catch (Exception ex)            
                {
                    throw new Exception("图像文件保存失败！：" + ex.Message);
                }        
            }        
            else        
            {
                throw new Exception("不支持的图像文件格式");
            }    
        }

        public void DeleteImageFile(BoPhotos photo)
        {
            String path = Server.MapPath("~/UploadImages/");
            String imagePath = path + photo.PHO_URL;
            System.IO.File.Delete(imagePath);
        }
    }
}